package com.xlt.activiti.one;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;

import java.util.List;

public class ActivitiSuspendProcessInstance {

    public static void suspendProcess(String instanceKey){
        // 1.得到processEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();

        // 3.得到查询器
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        // 4.设置查询条件

        // 5. 执行查询操作，查询出想要的流程定义
        ProcessDefinition processDefinition = processDefinitionQuery.
                processDefinitionKey(instanceKey).singleResult();


        String getDeploymentId =  processDefinition.getId();

        if(processDefinition!=null){
            boolean suspended = processDefinition.isSuspended();
            // 是否挂起
            if (suspended){
                // 激活
                repositoryService.activateProcessDefinitionById(getDeploymentId,true,null);
                System.out.println("激活");
            }else {
                // 挂起
                repositoryService.suspendProcessDefinitionById(getDeploymentId,true,null);
                System.out.println("挂启");
            }
        }
    }
}
